//
//  UINavigationItem+Item.m
//  CbonsBabyCare
//
//  Created by  on 15/10/27.
//  Copyright © 2015年 cbons. All rights reserved.
//

#import "UINavigationItem+Item.h"

@implementation UINavigationItem (Item)

- (void)setItem:(NSString * __nullable)title image:(NSString * __nullable)image highlightImage:(NSString * __nullable)highlightImage target:(id __nonnull)target selector:(SEL __null_unspecified)selector left:(BOOL)left
{
    UIBarButtonItem *item = nil;
    
    if (title.length > 0) {
        item = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:target action:selector];
        NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
        textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
        [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
        [item setTitleTextAttributes:textAttrs forState:UIControlStateHighlighted];
        item.tintColor = [UIColor whiteColor];
    }
    else if (image.length > 0)
    {
        item = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:image] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:target action:selector];
        [item setBackgroundImage:[[UIImage imageNamed:highlightImage] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    }
    
    if (left) {
        self.leftBarButtonItem  = item;
    }
    else
    {
        self.rightBarButtonItem = item;
    }
}

- (void)setItem:(NSString * __nullable)title image:(NSString * __nullable)image highlightImage:(NSString * __nullable)highlightImage target:(id __nonnull)target selector:(SEL __null_unspecified)selector color:(UIColor *__nullable)color left:(BOOL)left
{
    UIBarButtonItem *item = nil;
    
    if (title.length > 0) {
        item = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:target action:selector];
        NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
        textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
        [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
        [item setTitleTextAttributes:textAttrs forState:UIControlStateHighlighted];
         item.tintColor = color;
    }
    else if (image.length > 0)
    {
        item = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:image] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:target action:selector];
        [item setBackgroundImage:[[UIImage imageNamed:highlightImage] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    }
    
    if (left) {
        self.leftBarButtonItem = item;
    }
    else
    {
        self.rightBarButtonItem = item;
    }
}

- (void)setItem:(NSString * __nullable)title image:(NSString * __nullable)image highlightImage:(NSString * __nullable)highlightImage target:(id __nonnull)target selector:(SEL __null_unspecified)selector color:(UIColor *__nullable)color left:(BOOL)left enabled:(BOOL)enabled
{
    UIBarButtonItem *item = nil;
    if (title.length > 0) {
        item = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:target action:selector];
        NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
        textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
        [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
        [item setTitleTextAttributes:textAttrs forState:UIControlStateHighlighted];
        item.enabled =enabled;
        item.tintColor = color;
    }
    else if (image.length > 0)
    {
        item = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:image] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:target action:selector];
        [item setBackgroundImage:[[UIImage imageNamed:highlightImage] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    }
    
    if (left) {
        self.leftBarButtonItem = item;
    }
    else
    {
        self.rightBarButtonItem = item;
    }
}

@end
